import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JScrollPane;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextArea textShow;
	private JPanel panelOption;

	private JLabel labelResult_Offer;
	private JPanel panelOffer;
	private JTextField textValue_Offer;

	private JPanel panelShow;
	private JLabel labelMessage_Show;

	private LinkQueue linkQueue = new LinkQueue();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Queue");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 551, 435);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnClearCall();
			}
		});
		btnClear.setBounds(406, 32, 113, 27);
		contentPane.add(btnClear);

		JButton btnLength = new JButton("Length");
		btnLength.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnLengthCall();
			}
		});
		btnLength.setBounds(406, 91, 113, 27);
		contentPane.add(btnLength);

		JButton btnIsEmpty = new JButton("isEmpty");
		btnIsEmpty.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnIsEmptyCall();
			}
		});
		btnIsEmpty.setBounds(406, 150, 113, 27);
		contentPane.add(btnIsEmpty);

		JButton btnPeek = new JButton("Peek");
		btnPeek.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnPeekCall();
			}
		});
		btnPeek.setBounds(406, 209, 113, 27);
		contentPane.add(btnPeek);

		JButton btnOffer = new JButton("Offer");
		btnOffer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnOfferCall();
			}
		});
		btnOffer.setBounds(406, 268, 113, 27);
		contentPane.add(btnOffer);

		JButton btnPoll = new JButton("Poll");
		btnPoll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnPollCall();
			}
		});
		btnPoll.setBounds(406, 327, 113, 27);
		contentPane.add(btnPoll);

		JLabel lblNewLabel = new JLabel("Queue:");
		lblNewLabel.setBounds(14, 13, 54, 18);
		contentPane.add(lblNewLabel);

		panelOption = new JPanel();
		panelOption.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelOption.setBounds(14, 203, 363, 172);
		contentPane.add(panelOption);
		panelOption.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 33, 363, 146);
		contentPane.add(scrollPane);

		textShow = new JTextArea();
		scrollPane.setViewportView(textShow);
		textShow.setEditable(false);
		textShow.setLineWrap(true);
		textShow.setColumns(10);

		// --------------------------------------------------
		panelOffer = new JPanel();
		panelOffer.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelOffer.setLayout(null);
		panelOffer.setBounds(0, 0, 363, 172);

		JButton btnOffer_P = new JButton("Offer");
		btnOffer_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnOffer_PCall();
			}
		});
		btnOffer_P.setBounds(249, 41, 102, 27);
		panelOffer.add(btnOffer_P);

		JLabel label_1 = new JLabel("Value:");
		label_1.setBounds(10, 45, 56, 18);
		panelOffer.add(label_1);

		textValue_Offer = new JTextField();
		textValue_Offer.setColumns(10);
		textValue_Offer.setBounds(76, 42, 163, 24);
		panelOffer.add(textValue_Offer);

		labelResult_Offer = new JLabel();
		labelResult_Offer.setText("Input the value");
		labelResult_Offer.setHorizontalAlignment(SwingConstants.CENTER);
		labelResult_Offer.setBounds(14, 108, 335, 18);
		panelOffer.add(labelResult_Offer);

		// -----------------------------------
		panelShow = new JPanel();
		panelShow.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelShow.setLayout(null);
		panelShow.setBounds(0, 0, 363, 172);

		labelMessage_Show = new JLabel("Empty Message!");
		labelMessage_Show.setHorizontalAlignment(SwingConstants.CENTER);
		labelMessage_Show.setBounds(14, 77, 335, 18);
		panelShow.add(labelMessage_Show);

		// -------------------------------------
		showMessage("Choose an opetion!");
		showQueue();
	}

	// 清空按钮监听
	protected void btnClearCall() {
		linkQueue.clear();
		showQueue();
		showMessage("Cleared the queue!");
	}

	// 长度按钮监听
	protected void btnLengthCall() {
		int length = linkQueue.length();
		showMessage("Length of the queue is " + length + " !");
	}

	// 判空按钮监听
	protected void btnIsEmptyCall() {
		if (linkQueue.isEmpty()) {
			showMessage("The queue is empty!");
		} else {
			showMessage("The queue is not empty!");
		}
	}

	// 取队首元素按钮监听
	protected void btnPeekCall() {
		Object peek = linkQueue.peek();
		if (peek == null) {
			showMessage("The queue is empty!");
		} else {
			showMessage("Peek the " + peek.toString());
		}
	}

	// 入队按钮监听
	protected void btnOfferCall() {
		switchPanel("Offer");
	}

	// 入队元素按钮监听
	protected void btnOffer_PCall() {
		if (textValue_Offer.getText().length() < 1) {
			return;
		}
		linkQueue.offer(textValue_Offer.getText());
		showQueue();
		textValue_Offer.setText("");
		labelResult_Offer.setText("Offered!");
	}

	// 出队按钮监听
	protected void btnPollCall() {
		Object poll = linkQueue.poll();
		if (poll == null) {
			showMessage("The queue is empty!");
		} else {
			showMessage("Poll the " + poll.toString() + " !");
			showQueue();
		}
	}

	/**
	 * 切换功能框
	 * 
	 * @param panelName
	 *            需要切换的功能框名
	 */
	private void switchPanel(String panelName) {
		// 去除功能框内原有的功能框
		panelOption.removeAll();
		// 切换对应的功能框
		switch (panelName) {
		case "Offer":
			panelOption.add(panelOffer);
			break;
		case "Show":
			panelOption.add(panelShow);
			break;
		}
		// 通知重绘
		panelOption.repaint();
	}

	/**
	 * 显示一个消息或结果
	 * 
	 * @param message
	 *            需要显示的信息
	 */
	private void showMessage(String message) {
		// 设置显示的信息
		labelMessage_Show.setText(message);
		// 切换到消息功能框
		switchPanel("Show");
	}

	private void showQueue() {
		String qString = linkQueue.getString();
		textShow.setText(qString);
	}
}
